﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Cinser.Common.Wpf.Controls
{
    /// <summary>
    /// Pager.xaml 的交互逻辑
    /// </summary>
    public partial class Pager : UserControl
    {

        public event EventHandler PageChanged;
        int _recordCount;
        int _onePageItemCount;
        int _pageCount;
        int _currentPage = 1;

        public enum eViewType
        {
            SimpleView,     //简单视图
            CompleteView    //完全视图
        }
        
        public enum onePageItemCount
        {
            _10, _20, _30, _50, _100, _200
        }

        eViewType _ViewType = eViewType.CompleteView;

        public eViewType ViewType
        {
            get { return _ViewType; }
            set
            {
                _ViewType = value;
                if (ViewType == eViewType.CompleteView)
                {
                    this._lbInfo.Visibility = System.Windows.Visibility.Visible;
                    this._lbInfo2.Visibility = System.Windows.Visibility.Visible;
                    this._BtnFirst.Visibility = System.Windows.Visibility.Visible;
                    this._BtnLast.Visibility = System.Windows.Visibility.Visible;
                    this._cbbOnePageCount.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    this._lbInfo.Visibility = System.Windows.Visibility.Collapsed;
                    this._lbInfo2.Visibility = System.Windows.Visibility.Collapsed;
                    this._BtnFirst.Visibility = System.Windows.Visibility.Collapsed;
                    this._BtnLast.Visibility = System.Windows.Visibility.Collapsed;
                    this._cbbOnePageCount.Visibility = System.Windows.Visibility.Collapsed;

                }
            }
        }


        public int CurrentPage
        {
            get { return _currentPage; }
            set
            {

                this._BtnNext.IsEnabled = true;
                this._BtnLast.IsEnabled = true;
                this._BtnFirst.IsEnabled = true;
                this._BtnPrev.IsEnabled = true;

                _currentPage = value;
                //设置当前页
                if (this._currentPage >= this._pageCount)
                {
                    this._currentPage = this._pageCount;
                    this._BtnNext.IsEnabled = false;
                    this._BtnLast.IsEnabled = false;
                }
                if (this._currentPage <= 1)
                {
                    this._currentPage = 1;
                    this._BtnFirst.IsEnabled = false;
                    this._BtnPrev.IsEnabled = false;
                }

                OnPageChanged();
            }
        }

        public int OnePageItemCount
        {
            get { return _onePageItemCount; }
            private set
            {
                _onePageItemCount = value;
                if (this._onePageItemCount < 1)
                    this._onePageItemCount = 10; //如果每页记录数不正确，即更改为10
                InitPager();
            }
        }

        public Pager()
        {
            InitializeComponent();
            this._cbbOnePageCount.Items.Add("10");
            this._cbbOnePageCount.Items.Add("20");
            this._cbbOnePageCount.Items.Add("30");
            this._cbbOnePageCount.Items.Add("50");
            this._cbbOnePageCount.Items.Add("100");
            this._cbbOnePageCount.Items.Add("200");
        }

        public void InitPager(int recordCount, onePageItemCount onePageItemCount = onePageItemCount._20)
        {
            this._recordCount = recordCount;
            if (onePageItemCount == Pager.onePageItemCount._10)
            {
                this._onePageItemCount = 10;
                this._cbbOnePageCount.SelectedIndex = 0;                
            }
            else if (onePageItemCount == Pager.onePageItemCount._20)
            {
                this._onePageItemCount = 20;
                this._cbbOnePageCount.SelectedIndex = 1;
            }
            else if (onePageItemCount == Pager.onePageItemCount._30)
            {
                this._onePageItemCount = 30;
                this._cbbOnePageCount.SelectedIndex = 2;
            }
            else if (onePageItemCount == Pager.onePageItemCount._50)
            {
                this._onePageItemCount = 50;
                this._cbbOnePageCount.SelectedIndex = 3;
            }
            else if (onePageItemCount == Pager.onePageItemCount._100)
            {
                this._onePageItemCount = 100;
                this._cbbOnePageCount.SelectedIndex = 4;
            }
            else if (onePageItemCount == Pager.onePageItemCount._200)
            {
                this._onePageItemCount = 200;
                this._cbbOnePageCount.SelectedIndex = 5;
            }
            this._cbbOnePageCount.SelectionChanged += new SelectionChangedEventHandler(_cbbOnePageCount_SelectionChanged);
            InitPager();
        }

        private void InitPager()
        {
            if (this._recordCount < 0)
                this._recordCount = 0; //如果记录总数不正确，即更改为0

            //取得总页数
            if (this._recordCount % this.OnePageItemCount == 0)
                this._pageCount = this._recordCount / this.OnePageItemCount;
            else
                this._pageCount = this._recordCount / this.OnePageItemCount + 1;

            this._lbInfo.Content = string.Format("总记录数：{0}条   共{1}页   每页", this._recordCount, this._pageCount);

            this.CurrentPage = 1; //默认为第一页
        }
        
        private void OnPageChanged()
        {
            this._tbPageIndex.Text = this.CurrentPage.ToString();

            if (PageChanged != null)
            {
                PageChanged(this, null);
            }
        }

        private void _cbbOnePageCount_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.OnePageItemCount = int.Parse(_cbbOnePageCount.SelectedItem.ToString());
        }

        private void _BtnFirst_Click(object sender, RoutedEventArgs e)
        {
            this.CurrentPage = 1;
        }

        private void _BtnPrev_Click(object sender, RoutedEventArgs e)
        {
            this.CurrentPage = this.CurrentPage - 1;
        }

        private void _BtnGo_Click(object sender, RoutedEventArgs e)
        {
            this.CurrentPage = int.Parse(_tbPageIndex.Text);
        }

        private void _BtnNext_Click(object sender, RoutedEventArgs e)
        {
            this.CurrentPage = this.CurrentPage + 1;
        }

        private void _BtnLast_Click(object sender, RoutedEventArgs e)
        {
            this.CurrentPage = this._pageCount;
        }

        private void UserControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                _BtnGo_Click(_BtnGo, null);
            }
        }
    }
}
